package com.sacfa.commons.bo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;

import org.apache.log4j.Logger;

import com.sacfa.commons.beans.Email;
import com.sacfa.commons.beans.Opcion;
import com.sacfa.commons.connection.PoolConexion;
import com.sacfa.commons.sql.EmailSQL;
import com.sacfa.commons.utilities.SACFAExceptions;

/**
 * @author Leizar Zar
 * @version 1.0
 * @created 11-ago-2013
 */
public class EmailBo {
	private static Logger log = Logger.getLogger(EmailBo.class);
	
	public int registrar(Email email, Connection conex){
		int idEmail = 0;
		PreparedStatement stm = null;
		ResultSet rs = null;
		
		try {
			stm = conex.prepareStatement(EmailSQL.INSERT, Statement.RETURN_GENERATED_KEYS);
			stm.setString(1, email.getEmail());
			stm.setInt(2, email.getTipoEmail().getId());
			stm.executeUpdate();
			
			rs = stm.getGeneratedKeys();
			rs.next();
			idEmail = rs.getInt(1);
		} catch(SQLException ex) {
			log.info("Error SQL", ex);
			throw new SACFAExceptions("Error SQL", ex);
		}
		return idEmail;
	}
	
	public void registrarEmailCliente(int idCliente, int idEmail, Connection conex){
		PreparedStatement stm = null;
		
		try {
			stm = conex.prepareStatement(EmailSQL.INSERT_CLIENTE);
			stm.setInt(1, idCliente);
			stm.setInt(2, idEmail);
			stm.executeUpdate();
		} catch(SQLException ex) {
			log.info("Error SQL", ex);
			throw new SACFAExceptions("Error SQL", ex);
		} 
	}
	
	public void registrarEmailProveedor(int idProveedor, int idEmail, Connection conex){
		PreparedStatement stm = null;
		
		try {
			stm = conex.prepareStatement(EmailSQL.INSERT_PROVEEDOR);
			stm.setInt(1, idProveedor);
			stm.setInt(2, idEmail);
			stm.executeUpdate();
		} catch(SQLException ex) {
			log.info("Error SQL", ex);
			throw new SACFAExceptions("Error SQL", ex);
		} 
	}
	
	public void registrarEmailEmpleado(int idEmpleado, int idEmail, Connection conex){
		PreparedStatement stm = null;
		
		try {
			stm = conex.prepareStatement(EmailSQL.INSERT_EMPLEADO);
			stm.setInt(1, idEmpleado);
			stm.setInt(2, idEmail);
			stm.executeUpdate();
		} catch(SQLException ex) {
			log.info("Error SQL", ex);
			throw new SACFAExceptions("Error SQL", ex);
		}
	}

	public Email consultar(Email email){
		Opcion opcion = new Opcion();
		PreparedStatement stm = null;
		ResultSet rs = null;
		Connection conex = PoolConexion.getInstance().getConexion();
		
		try {
			stm = conex.prepareStatement(EmailSQL.SELECT_ONE);
			stm.setInt(1, email.getId());
			rs = stm.executeQuery();
			
			rs.next();
			email.setId(rs.getInt(1));
			email.setEmail(rs.getString(2));
			opcion.setId(rs.getInt(3));
			email.setTipoEmail(opcion);
		} catch(SQLException ex) {
			log.info("Error SQL", ex);
			throw new SACFAExceptions("Error SQL", ex);
		} finally {
			PoolConexion.getInstance().endConection(conex);
		}
		return email;
	}
	
	public Collection<Email> consultaEmailsCliente(int idCliente){
		Collection<Email> emails = new ArrayList<Email>();
		Email email = new Email();
		Opcion opcion = new Opcion();
		PreparedStatement stm = null;
		ResultSet rs = null;
		Connection conex = PoolConexion.getInstance().getConexion();
		
		try {
			stm = conex.prepareStatement(EmailSQL.SELECT_ALL_CLIENTE);
			stm.setInt(1, idCliente);
			rs = stm.executeQuery();
			
			while(rs.next()){
				email.setId(rs.getInt(1));
				email.setEmail(rs.getString(2));
				opcion.setId(rs.getInt(3));
				email.setTipoEmail(opcion);
				emails.add(email);
			}
		} catch(SQLException ex) {
			log.info("Error SQL", ex);
			throw new SACFAExceptions("Error SQL", ex);
		} finally {
			PoolConexion.getInstance().endConection(conex);
		}
		return emails;
	}
	
	public Collection<Email> consultaEmailsProveedor(int idProveedor){
		Collection<Email> emails = new ArrayList<Email>();
		Email email = new Email();
		Opcion opcion = new Opcion();
		PreparedStatement stm = null;
		ResultSet rs = null;
		Connection conex = PoolConexion.getInstance().getConexion();
		
		try {
			stm = conex.prepareStatement(EmailSQL.SELECT_ALL_PROVEEDOR);
			stm.setInt(1, idProveedor);
			rs = stm.executeQuery();
			
			while(rs.next()){
				email.setId(rs.getInt(1));
				email.setEmail(rs.getString(2));
				opcion.setId(rs.getInt(3));
				email.setTipoEmail(opcion);
				emails.add(email);
			}
		} catch(SQLException ex) {
			log.info("Error SQL", ex);
			throw new SACFAExceptions("Error SQL", ex);
		} finally {
			PoolConexion.getInstance().endConection(conex);
		}
		return emails;
	}
	
	public Collection<Email> consultaEmailsEmpleado(int idEmpleado){
		Collection<Email> emails = new ArrayList<Email>();
		Email email = new Email();
		Opcion opcion = new Opcion();
		PreparedStatement stm = null;
		ResultSet rs = null;
		Connection conex = PoolConexion.getInstance().getConexion();
		
		try {
			stm = conex.prepareStatement(EmailSQL.SELECT_ALL_EMPLEADO);
			stm.setInt(1, idEmpleado);
			rs = stm.executeQuery();
			
			while(rs.next()){
				email.setId(rs.getInt(1));
				email.setEmail(rs.getString(2));
				opcion.setId(rs.getInt(3));
				email.setTipoEmail(opcion);
				emails.add(email);
			}
		} catch(SQLException ex) {
			log.info("Error SQL", ex);
			throw new SACFAExceptions("Error SQL", ex);
		} finally {
			PoolConexion.getInstance().endConection(conex);
		}
		return emails;
	}
	
	public void modificar(Email email){
		PreparedStatement stm = null;
		Connection conex = PoolConexion.getInstance().getConexion();
		
		try {
			PoolConexion.getInstance().beginTransaction(conex);
			
			stm = conex.prepareStatement(EmailSQL.UPDATE);
			stm.setString(1, email.getEmail());
			stm.setInt(2, email.getTipoEmail().getId());
			stm.executeUpdate();
			
			PoolConexion.getInstance().commit(conex);
			PoolConexion.getInstance().endTransaction(conex);
		} catch(SQLException ex) {
			log.info("Error SQL", ex);
			throw new SACFAExceptions("Error SQL", ex);
		} finally {
			PoolConexion.getInstance().endConection(conex);
		}
	}
	
	public void eliminar(Email email){
		PreparedStatement stm = null;
		Connection conex = PoolConexion.getInstance().getConexion();
		
		try {
			PoolConexion.getInstance().beginTransaction(conex);
			
			stm = conex.prepareStatement(EmailSQL.DELETE);
			stm.setInt(1, email.getId());
			stm.executeUpdate();
			
			PoolConexion.getInstance().commit(conex);
			PoolConexion.getInstance().endTransaction(conex);
		} catch(SQLException ex) {
			log.info("Error SQL", ex);
			throw new SACFAExceptions("Error SQL", ex);
		} finally {
			PoolConexion.getInstance().endConection(conex);
		}
	}
	
	public void eliminarEmailCliente(int idCliente, int idEmail){
		PreparedStatement stm = null;
		Connection conex = PoolConexion.getInstance().getConexion();
		
		try {
			PoolConexion.getInstance().beginTransaction(conex);
			
			stm = conex.prepareStatement(EmailSQL.DELETE_CLIENTE);
			stm.setInt(1, idCliente);
			stm.setInt(2, idEmail);
			stm.executeUpdate();
			
			PoolConexion.getInstance().commit(conex);
			PoolConexion.getInstance().endTransaction(conex);
		} catch(SQLException ex) {
			log.info("Error SQL", ex);
			throw new SACFAExceptions("Error SQL", ex);
		} finally {
			PoolConexion.getInstance().endConection(conex);
		}
	}
	
	public void eliminarEmailProveedor(int idProvedoor, int idEmail){
		PreparedStatement stm = null;
		Connection conex = PoolConexion.getInstance().getConexion();
		
		try {
			PoolConexion.getInstance().beginTransaction(conex);
			
			stm = conex.prepareStatement(EmailSQL.DELETE_PROVEEDOR);
			stm.setInt(1, idProvedoor);
			stm.setInt(2, idEmail);
			stm.executeUpdate();
			
			PoolConexion.getInstance().commit(conex);
			PoolConexion.getInstance().endTransaction(conex);
		} catch(SQLException ex) {
			log.info("Error SQL", ex);
			throw new SACFAExceptions("Error SQL", ex);
		} finally {
			PoolConexion.getInstance().endConection(conex);
		}
	}
	
	public void eliminarEmailEmpleado(int idEmpleado, int idEmail){
		PreparedStatement stm = null;
		Connection conex = PoolConexion.getInstance().getConexion();
		
		try {
			PoolConexion.getInstance().beginTransaction(conex);
			
			stm = conex.prepareStatement(EmailSQL.DELETE_EMPLEADO);
			stm.setInt(1, idEmpleado);
			stm.setInt(2, idEmail);
			stm.executeUpdate();
			
			PoolConexion.getInstance().commit(conex);
			PoolConexion.getInstance().endTransaction(conex);
		} catch(SQLException ex) {
			log.info("Error SQL", ex);
			throw new SACFAExceptions("Error SQL", ex);
		} finally {
			PoolConexion.getInstance().endConection(conex);
		}
	}
}
